package com.atzxh.exer;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 小红同学
 * @create 2023-05-03-21:54
 * @description
 */
public class StreamTest {
    public static void main(String[] args) {
        List<Students> students = new ArrayList<>();
        students.add(new Students(1, "Tom", 18, 89));
        students.add(new Students(2, "Lucy", 19, 95));
        students.add(new Students(3, "Jack", 20, 80));
        students.add(new Students(4, "Mary", 18, 92));
        students.add(new Students(5, "Bob", 19, 85));

        // map：将学生集合中每个学生的分数乘以2，得到一个新的集合。
        List<Integer> scores = students.stream()
                .map(student -> student.getScore() * 2)
                .collect(Collectors.toList());

        // filter：过滤出年龄大于等于19岁的学生，得到一个新的集合。
        List<Students> filteredStudents = students.stream()
                .filter(student -> student.getAge() >= 19)
                .collect(Collectors.toList());

        // flatMap：将学生集合中每个学生的名字拆分成字符数组，得到一个新的集合
        List<String> names = students.stream()
                .flatMap(student -> Arrays.stream(student.getName().split("")))
                .collect(Collectors.toList());

        // distinct：将学生集合中所有不重复的年龄值取出，得到一个新的集合
        List<Integer> ages = students.stream()
                .map(Students::getAge)
                .distinct()
                .collect(Collectors.toList());

        // count：统计学生集合中的学生数量
        long count = students.stream().count();

        // max：找出学生集合中分数最高的学生。
        Optional<Students> minStudent = students.stream()
                .max(Comparator.comparing(Students::getScore));
    }
}
class Students {
    private int id;
    private String name;
    private int age;
    private int score;

    public Students(int id, String name, int age, int score) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}
